/**
 * @desc 接口模块 api模块
 */

const express = require('express')
const router = express.Router()
const User = require('../models/user')
const Content = require('../models/content')
const common = require('../util/common')

console.log()
// 返回同一格式
let responseData

router.use((req, res, next) => {
  responseData = {
    code: 0,
    message: ''
  }
  next()
})

// 处理注册接口
router.post('/user/register', (req, res) => {
  const username = req.body.username
  const password = req.body.password
  // 在数据库查找是否存在当前同户名
  User.findOne({
    username
  }).then(data => {
    if (!data) { // 1 不存在
      // 1.1 添加到数据库中
      let user = new User({
        username,
        password
      })
      return user.save()
    } else { // 2 存在 告诉用户用户名重复
      responseData.code = -1
      responseData.message = '该用户名已存在, 去登陆'
      res.status(200).json(responseData)
    }

  }).then(function(data) {
    console.log(data)
    // 1.2 告诉用户添加成功
    if (!!data) {
      responseData.code = 0
      responseData.message = '添加成功'
      res.status(200).json(responseData)
    } else {
      responseData.code = -1
      responseData.message = '数据库储存失败'
      res.status(400).json(responseData)
    }
  })
})

// 处理登陆接口
router.post('/user/login', (req, res) => {
  let username = req.body.username
  let password = req.body.password
	if(!username||!password){
		let USERSTR = ''
		req.on('data', chunk => {
			USERSTR += chunk
		} )
		req.on('end', () => {
			// console.log(str)
			username = JSON.parser(USERSTR).username
			password = JSON.parser(USERSTR).password
		})
	}
	console.log(username, password)
  // 在数据库查找是否存在当前同户名
  User.findOne({
    username,
    password
  }).then((data) => {
    if (!!data) { // 登陆成功
      responseData.code = 0
      responseData.message = '登陆成功'
      responseData.data = {
        id: data._id,
        username: data.username
      }
      req.cookies.set('userInfo', JSON.stringify(responseData.data))
      res.status(200).json(responseData)
    } else { // 用户名或密码错误
      responseData.code = -1
      responseData.message = '用户名或密码错误'
    }
    res.status(400).json(responseData)
  })
})

// 处理推出的接口
router.get('/user/logout', (req, res) => {
  // 删除cookie
  req.cookies.set('userInfo', null)
  res.status(200).send({
    code: 0,
    message: '退出成功!'
  })
})

// 获取评论
router.get('/comment', (req, res) => {
  let {
    id
  } = req.query
  Content.findOne({
    _id: id
  }).then(content => {
    console.log(content)
    responseData.message = '请求成功!'
    responseData.data = content.comments.reverse()
    res.json(responseData)
  })
})

// 提交评论
router.post('/comment/post', (req, res) => {
  // 评论人
  // 评论内容
  // 评论时间
  let contentId = req.body.contentid || ''
  let postDate = {
    username: req.userInfo.username,
    postTime: new Date(),
    comtent: req.body.content
  }
  Content.findOne({
    _id: contentId
  }).then(content => {
    console.log(content)
    content.comments.push(postDate)
    return content.save()
  }).then(newContent => {
    responseData.message = '评论成功!'
    responseData.data = newContent.comments.reverse()
    res.json(responseData)
  })
})

module.exports = router
